﻿using System;
using System.Collections.Generic;
using System.Linq;
using MATtutsLab.Entities;
using Norm;

namespace MATtutsLab.Repositories
{
    public class MovieRepository : IMovieRepository
    {
        public Movie GetByTitle(string title)
        {
            using(var session = new MongoSession())
            {
                var movie = session.Single<Movie>(m => m.Title.ToLower() == title.ToLower());

                return movie;
            }
        }

        public Movie GetById(ObjectId id)
        {
            using (var session = new MongoSession())
            {
                var movie = session.Single<Movie>(m => m.Id == id);

                return movie;
            }
        }

        public IList<Movie> GetAll()
        {
            using (var session = new MongoSession())
            {
                var movies = session.All<Movie>().ToList();

                return movies;
            }
        }

        public void Save(Movie movie)
        {
            using (var session = new MongoSession())
            {
                //save
                if (movie.Id == null)
                    session.Add(movie);
                //update
                else
                    session.Update(movie);
            }
        }

        public void Delete(Movie movie)
        {
            using (var session = new MongoSession())
            {
                session.Delete(movie);
            }
        }
    }
}
